/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rdr.geo.map2d.tiles;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ruanor
 */
public class TileManager {

    private List<Tile> tiles;

    public TileManager() {
        tiles = new ArrayList<Tile>();
    }

    public void loadTiles(String tilesDirPath) {
        String[] xmlFiles = getListOfFiles(tilesDirPath, ".xml");

        for (String file : xmlFiles) {
            file = tilesDirPath + File.separator + file;
            Tile newTile = new TileCreator().create(file);
            addTile(newTile);
        }

        System.out.println(xmlFiles.length);
    }

    public void addTile(Tile tile) {
        tiles.add(tile);
    }

    public List<Tile> getTiles() {
        return tiles;
    }

    private String[] getListOfFiles(String aDirectory, final String extension) {
        // our filename filter (filename pattern matcher)
        class onlyExtension implements FilenameFilter {

            @Override
            public boolean accept(File dir, String s) {
                if (s.toLowerCase().endsWith(extension.toLowerCase())) {
                    return true;
                }
                return false;
            }
        }
        return new java.io.File(aDirectory).list(new onlyExtension());
    }
}
